/*
 * @Author: 晴鸢. 3013433857@qq.com
 * @Date: 2025-08-30 10:36:51
 * @LastEditors: 晴鸢. 3013433857@qq.com
 * @LastEditTime: 2025-08-31 11:54:45
 * @FilePath: /Learn/fifo/server.cc
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

 #include"common.hpp"
 #include"log.hpp"
using namespace std;

//管理管道文件 
int main()
{    
    Log log;
    Init init;
    log.Enable(Classfile);
    //打开管道 
    //等待写入方打开以后，自己才会打开文件，向后执行
    int fd=open(FIFO_FILE,O_RDONLY);
    if(fd<0)
    {
        log(Fatal,"server error.error string:%s, error code:%d\n",strerror(errno),errno);
        exit(FIFO_OPEN_ERR);
    }

    log(Info,"server open file done,error string：%s,error code：%d\n",strerror(errno),errno);
    //开始通信
    while(1)
    {
        char buffer[1024]={0};
        int x=read(fd,buffer,sizeof(buffer));
        if(x>0)
        {
            buffer[x]=0;
            cout<<"client say# "<<buffer<<endl;
        }
        else if(x==0)//写入端关闭，读端也关闭
        {
           log(Debug,"client quit,me too!error string：%s,error code：%d\n",strerror(errno),errno);
            break;
        }
        else break;
    }
    close(fd);

    return 0;
}